import { Metadata } from "next";
import "./index.css";

// 导入共享的getProduct函数
import { getProduct } from "./page";

// 生成元数据
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
  try {
    const { slug } = await params;
    const product = await getProduct(slug);

    if (!product) {
      return {
        title: "产品不存在 - AI产品之家",
        description: "该产品可能已被删除或移动",
      };
    }

    // 安全地处理content字段
    const cleanContent = product.content ? product.content.replace(/<[^>]*>/g, "").substring(0, 160) : "";
    const description = product.summary || cleanContent || `${product.title} - 发现好用的AI产品`;

    return {
      title: `${product.title} - AI产品之家`,
      description,
      keywords: product.category?.name || "",
      openGraph: {
        title: product.title,
        description,
        type: "website",
        images: product.coverImage
          ? [
              {
                url: product.coverImage,
                width: 800,
                height: 600,
                alt: product.title,
              },
            ]
          : undefined,
      },
    };
  } catch (error) {
    console.error("Failed to generate metadata:", error);
    return {
      title: "产品详情 - AI产品之家",
      description: "AI产品之家 - 发现好用的AI产品",
    };
  }
}

export default function ProductDetailLayout({ children }: { children: React.ReactNode }) {
  return children;
}
